package com.bite.book.service;

//import com.bite.book.dao.BookDao;
import com.bite.book.enums.BookStatusEnum;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service//把对象交给spring管理
public class BookService {

//    @Autowired//从spring获取对象，并给对象复制
//    private BookDao bookDao;

    @Autowired
    private BookMapper bookMapper;

//    public List<BookInfo> getList() {
//        //理应从数据库中查询图书信息，但未学习数据库相关操作，暂且mock数据
//        List<BookInfo> bookInfos = bookDao.mockData();
//        for(BookInfo bookInfo:bookInfos){
//            if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else{
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public void addBook(BookInfo bookInfo) {
       bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1、获得总图书数
        //2、获得当前页数据
        Integer count= bookMapper.count();
         List<BookInfo> bookInfos=bookMapper.selectBooksByPage(pageRequest);
        //3、对结果进行二次处理
        for(BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());


        }
        return  new ResponseResult<>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }



    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids){
        return bookMapper.batchDelete(ids);
    }
}
